package com.sy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.*;
import com.sy.entity.AdminUser;
import com.sy.entity.Coach;
import com.sy.entity.Member;
import com.sy.entity.Subject;
import com.sy.exception.BusinessException;
import com.sy.service.AdminUserService;
import com.sy.vo.AdminVo;
import com.sy.vo.CoachVo;
import com.sy.vo.MemberVo;
import com.sy.vo.SubjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.SESSION_VALIDATE_CODE;

@RestController
@RequestMapping("adminuser")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    @RequestMapping("adminlogin")
    public AdminUser adminlogin(HttpSession session, AdminUser adminUser, String validateCode) {
        String sessionCode =(String) session.getAttribute(SESSION_VALIDATE_CODE);
        System.out.println(sessionCode);
        if (!Objects.equals(sessionCode, validateCode)) {
            System.out.println(validateCode);
            throw new BusinessException(HttpStatus.FORBIDDEN, "验证码错误");
        }
        AdminUser byLogin = adminUserService.findByLogin(adminUser);
        if (byLogin != null) {
            session.setAttribute("loginAdminUser", byLogin);
            return byLogin;
        }
        throw new BusinessException(HttpStatus.NOT_FOUND,"用户名或密码错误");
    }

    @RequestMapping("getorders")
    public List<OrdersDto> getOrders(HttpSession session,Member member) {
        System.out.println(session.getAttribute("loginAdminUser"));
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        List<OrdersDto> allMemberOrders = adminUserService.findAllMemberOrders(member);
        return allMemberOrders;
    }

    @RequestMapping("createSub")
    public boolean createSubject(Subject subject, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        boolean b = adminUserService.saveSubject(subject);
        return b;
    }

    @RequestMapping("removeSub")
    public boolean removeSubject(Subject subject, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        boolean b = adminUserService.dropSubject(subject);
        return b;
    }

    @RequestMapping("searchSub")
    public SubjectDto searchSubByAdminVo(SubjectDto subjectDto, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        PageHelper.startPage(Integer.valueOf(subjectDto.getPageNum()),Integer.valueOf(subjectDto.getPageSize()));
        Page<SubjectDto> allSubjects =(Page<SubjectDto>) adminUserService.findBySubjectName(subjectDto);
        System.out.println(allSubjects.getResult());
        long total = allSubjects.getTotal();
        SubjectDto result = new SubjectDto();
        result.setSubjectList(allSubjects);
        result.setSubjectTotal(total);
        return result;
    }

    @RequestMapping("searchOrderByUserName")
    public OrdersResult searchOrderByUserName(MemberVo memberVo, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        PageHelper.startPage(Integer.valueOf(memberVo.getPageNum()),Integer.valueOf(memberVo.getPageSize()));
        Page<OrdersDto> allOrders =(Page<OrdersDto>) adminUserService.findAllOrdersByMemberName(memberVo);
        long total = allOrders.getTotal();
        OrdersResult result = new OrdersResult();
        result.setOrderTotal(total);
        result.setOrdersList(allOrders);
        return result;
    }

    @RequestMapping("createCoach")
    public boolean createCoach(Coach Coach, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        boolean b = adminUserService.saveCoach(Coach);
        return b;
    }

    @RequestMapping("removeCoach")
    public boolean removeCoach(Coach Coach, HttpSession session) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        boolean b = adminUserService.dropCoach(Coach);
        return b;
    }
    @RequestMapping("getmembers")
    public MemberDto getMemberList(HttpSession session, MemberVo memberVo) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        System.out.println(memberVo);
        PageHelper.startPage(Integer.valueOf(memberVo.getPageNum()),Integer.valueOf(memberVo.getPageSize()));
        Page<Member> allMembers =(Page<Member>) adminUserService.getAllMembers(memberVo);
        long total = allMembers.getTotal();
        List<Member> result = allMembers.getResult();
        result.forEach(System.out::println);
        System.out.println("++++++++++++++");
        MemberDto memberDto = new MemberDto();
        memberDto.setMemberList(allMembers);
        memberDto.setMemberTotal(total);
        return memberDto;
    }

    @RequestMapping("getmemberById")
    public MemberDto getMemberById(HttpSession session, Integer id) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        MemberDto memberById = adminUserService.getMemberById(id);
        return memberById;
    }

    @RequestMapping("updateMember")
    public Boolean updateMember(HttpSession session,MemberDto memberDto) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }

        System.out.println(memberDto);
        Boolean aBoolean = adminUserService.updateMember(memberDto);
        return aBoolean;
    }
    @RequestMapping("dropMember")
    public Boolean dropMember(HttpSession session,MemberDto memberDto) {
        System.out.println(memberDto);
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        Boolean aBoolean = adminUserService.dropMember(memberDto);
        return aBoolean;
    }
    @RequestMapping("addMember")
    public Boolean addMember(HttpSession session,Member member){
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        boolean aBoolean = adminUserService.saveMember(member);
        return aBoolean;
    }

    @RequestMapping("getcoachs")
    public CoachDto getCoachList(HttpSession session, CoachVo CocahVo) {
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        System.out.println(CocahVo);
        PageHelper.startPage(Integer.valueOf(CocahVo.getPageNum()),Integer.valueOf(CocahVo.getPageSize()));
        Page<Coach> allCoachs =(Page<Coach>) adminUserService.getAllCoachs(CocahVo);
        long total = allCoachs.getTotal();
        CoachDto coachDto = new CoachDto();
        coachDto.setCoachList(allCoachs);
        coachDto.setCoachTotal(total);
        return coachDto;
    }

    @RequestMapping("removeOrder")
    public Boolean removeOrder(HttpSession session,OrdersDto ordersDto) {
        System.out.println(ordersDto);
        if (session.getAttribute("loginAdminUser") == null) {
            throw new BusinessException(HttpStatus.NOT_FOUND,"账号未登录");
        }
        Boolean aBoolean = adminUserService.dropOrder(ordersDto);
        return aBoolean;
    }

}
